<template>
  <div class="carItem">
    <div class="cars-item" @click="showDetail">
      <div class="car-header">
        <!-- <img src="../../../assets/images/cars-logo.png" alt="" /> -->
        <img
          :src="carData.brand_logo"
          alt=""
          style="width: 70px; height: 34px"
        />
        <div class="car-name">
          <span class="car-type">{{ carData.model }}</span>
          <span class="car-size">{{ car_type }}汽车 5座</span>
        </div>
      </div>
      <div class="car-body">
        <div class="car-info">
          <h5 class="car-plate">{{ carData.number }}</h5>
          <div class="car-h">
            <ul class="car-ele">
              <li class="ele-active" v-for="i of car_ele" :key="i"></li>
              <li v-for="index of no_car_ele" :key="index * 10"></li>
            </ul>
            <span class="car-minter">
              约 <b>{{ carData.km }}</b> KM
            </span>
          </div>
        </div>
        <!-- <img src="../../../assets/images/pic001.jpg" alt="" /> -->
        <img :src="carData.car_image" alt="" style="width: 48%" />
      </div>
      <div class="car-foot">
        <div class="car-part">{{ carData.parkingName }}</div>
        <i class="el-icon-arrow-right"></i>
      </div>
    </div>
    <div
      class="cars-item car-detail"
      v-if="car_info_show"
      :style="'height:' + car_info_height"
    >
      <div class="car-title">
        <div class="car-tpart">{{ carData.parkingName }}</div>
        <i class="el-icon-close" @click="closeDetail"></i>
      </div>
      <div class="car-header">
        <!-- <img src="../../../assets/images/cars-logo.png" alt="" /> -->
        <img
          :src="carData.brand_logo"
          alt=""
          style="width: 70px; height: 34px"
        />
        <div class="car-name">
          <span class="car-type">{{ carData.model }}</span>
          <span class="car-size">{{ car_type }}汽车 5座</span>
        </div>
      </div>
      <div class="car-img">
        <!-- <img src="../../../assets/images/pic001.jpg" alt=""> -->
        <img :src="carData.car_image" alt="" style="width: 80%; margin: auto" />
      </div>
      <div class="car-detail-info">
        <div class="car-name">
          <span class="name">{{ carData.number }}</span>
          <span class="m"
            >约 <b>{{ carData.km }}</b> KM</span
          >
        </div>
        <div class="car-ele">
          <div class="ele-no">
            <div
              class="ele"
              :style="{ width: (carData.km / 672) * 100 + '%' }"
            ></div>
          </div>
        </div>
        <div class="car-tip">取车约支付12.0元停车费，实际补贴12.0元</div>
        <ul class="car-price-list">
          <li @click="choosePrice" class="price-li">
            <span>日租车</span>
            <span>￥{{ carData.dRent }}/天</span>
          </li>
          <li @click="choosePrice" class="price-li">
            <span>15日租车</span>
            <span>￥{{ parseInt((carData.dRent * 15) / 15) }}/天</span>
          </li>
          <li @click="choosePrice" class="price-li">
            <span>月租车</span>
            <span>￥{{ parseInt(carData.mRent / 30) }}/天</span>
          </li>
          <li @click="choosePrice" class="price-li">
            <span>小时租车</span>
            <span>￥{{ carData.hRent }}/小时</span>
          </li>
        </ul>
      </div>
      <div class="car-deal">
        <span>参保《全面保障服务》用车更放心</span>
        <i
          class="el-icon-success"
          :style="{ color: isChecked ? '#000' : '#cccccc' }"
          @click="isChecked = !isChecked"
        ></i>
      </div>
      <div class="car-use" @click="useCar(carData.id)">预约用车</div>
    </div>
    
  </div>
</template>

<script>
import { mapActions, mapState } from 'vuex';
export default {
  name: "CarItem",
  props: ["carData"],
  data() {
    return {
      car_info_show: false,
      car_info_height: 0,
      timer: null,
      // 勾选服务
      isChecked: false,
      // 租赁类型
      leaseType: {
        type: "",
        price: "",
      },
      id: this.$store.state.userAbout.user_id,
    };
  },
  methods: {
    // 显示详情
    showDetail() {
      this.car_info_show = true;
      this.timer && clearTimeout(this.timer);
      this.timer = setTimeout(() => {
        this.car_info_height = "800px";
        clearTimeout(this.timer);
      }, 20);
    },
    // 关闭详情
    closeDetail() {
      this.car_info_show = false;
      this.car_info_height = 0;
    },
    // 选择价格
    choosePrice() {
      const _this = this;
      const price_list = document.querySelectorAll(".price-li");
      price_list.forEach((item) => {
        item.addEventListener("click", function () {
          price_list.forEach((ele) => {
            ele.className = "price-li";
          });
          this.className = "price-li price-active";
          _this.leaseType.type = this.children[0].innerHTML;
          _this.leaseType.price = this.children[1].innerHTML;
        });
      });
    },
    // 预约用车
    useCar(id) {
      if (!this.leaseType.type) {
        this.$message({ type: "warning", message: "请选择租车类型" });
      } else if (!this.isChecked) {
        this.$message({
          type: "warning",
          message: "请勾选《全面保障服务》协议",
        });
      } else {
        !this.$store.state.userAbout.token && this.$message({ type: "warning", message: "您还未登录，请先登录" });
        // this.$store.state.userAbout.token ? this.IsCompleteCertification(this.id) : this.$message({ type: "warning", message: "您还未登录，请先登录" });
      }
      // 预约
      this.IsCompleteCertification(this.id)
      let data = {
        type: this.leaseType.type,
        price: this.leaseType.price,
        id
      }
      this.authentication && this.OrderCar(data)

    },
    ...mapActions('userAbout', ['IsCompleteCertification']),
    ...mapActions('orderAbout', ['OrderCar'])
  },
  computed: {
    car_type() {
      return this.carData.energy === 1
        ? "新能源"
        : this.carData.energy === 2
        ? "燃油"
        : "混合动力";
    },
    car_ele() {
      return this.carData.energy === 1
        ? parseInt(this.carData.emass / 10)
        : this.carData.energy === 2
        ? parseInt(this.carData.omass / 10)
        : parseInt((this.carData.emass + this.carData.omass) / 20);
    },
    no_car_ele() {
      return 10 - this.car_ele;
    },
    ...mapState('userAbout', ['authentication'])
  },
};
</script>

<style lang="scss" scoped>
.carItem {
  position: relative;
  .cars-item {
    cursor: pointer;
    width: 505px;
    height: 258px;
    background-color: #fff;
    border-radius: 30px;
    padding: 30px 30px 20px 30px;
    box-sizing: border-box;
    box-shadow: 1px 0 18px 0 rgba(0, 0, 0, 0.2);
    .car-header {
      img {
        display: inline-block;
        vertical-align: middle;
        margin-right: 12px;
        position: relative;
      }
      .car-name {
        display: inline-block;
        .car-type {
          margin-right: 140px;
          font-size: 16px;
        }
        .car-size {
          font-size: 14px;
          color: #adaeb0;
          position: absolute;
          right: 36px;
        }
      }
    }
    .car-body {
      margin-top: 8px;
      width: 100%;
      height: 120px;
      display: flex;
      justify-content: space-between;
      border-bottom: 2px solid #e1e1e1;
      .car-info {
        .car-plate {
          font-size: 18px;
          font-weight: 400;
          margin-top: 20px;
          margin-bottom: 20px;
        }
        .car-h {
          display: flex;
        }
        .car-ele {
          display: inline-block;
          margin-right: 8px;
        }
        .car-ele li {
          display: inline-block;
          width: 8px;
          height: 24px;
          border-radius: 12px;
          margin-right: 3px;
          background-color: #e1e1e1;
        }
        .ele-active {
          background: linear-gradient(#11a5f8, #0c8ddb);
        }
        .car-minter {
          font-size: 12px;
          line-height: 31px;
          b {
            font-size: 16px;
          }
        }
      }
    }
    .car-foot {
      width: 100%;
      display: flex;
      justify-content: space-between;
      margin-top: 18px;
      color: #6e7174;
      font-size: 14px;
      line-height: 22px;
      i {
        color: #e1e1e1;
        font-size: 20px;
      }
    }
  }
  .car-detail {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    @keyframes showUp {
      0% {
        height: 0;
      }
    }
    animation: showUp 0.3s ease 0s;
    cursor: auto;
    .car-title {
      font-size: 18px;
      color: #6e7174;
      display: flex;
      line-height: 24px;
      justify-content: space-between;
      margin-bottom: 20px;
      i {
        font-size: 24px;
        cursor: pointer;
      }
    }
    .car-img {
      width: 100%;
      height: 200px;
      img {
        width: 100%;
        height: 100%;
      }
    }
    .car-detail-info {
      .car-name {
        display: flex;
        justify-content: space-between;
        .name {
          font-size: 20px;
          color: #34393f;
        }
        .m {
          font-size: 12px;
          b {
            font-size: 18px;
          }
        }
      }
      .car-ele {
        height: 40px;
        width: 100%;
        border-radius: 20px;
        background-color: #fff;
        padding: 15px 12px;
        box-sizing: border-box;
        box-shadow: 1px 0 8px 0 rgba(0, 0, 0, 0.2);
        margin-top: 20px;
        .ele-no {
          background-color: #e1e1e1;
          width: 100%;
          height: 100%;
          border-radius: 20px;
          position: relative;
          .ele {
            background-color: #0f9cee;
            width: 70%;
            height: 100%;
            border-radius: 20px;
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
          }
        }
      }
      .car-tip {
        font-size: 14px;
        text-align: center;
        color: #6e7174;
        margin-top: 20px;
        margin-bottom: 15px;
      }
      .car-price-list {
        margin-bottom: 10px;
        li {
          background-color: #f3f3f3;
          height: 45px;
          box-sizing: border-box;
          border-radius: 8px;
          line-height: 45px;
          display: flex;
          justify-content: space-between;
          margin-bottom: 15px;
          padding-right: 20px;
          padding-left: 15px;
          cursor: pointer;
        }
        .price-active {
          border: 2px solid #0f9cee;
        }
      }
    }
    .car-deal {
      margin-top: 30px;
      font-size: 14px;
      padding-left: 5px;
      padding-right: 5px;
      display: flex;
      line-height: 20px;
      justify-content: space-between;
      i {
        font-size: 20px;
        color: #cccccc;
      }
    }
    .car-use {
      font-size: 26px;
      background-color: #353b3f;
      text-align: center;
      line-height: 84px;
      height: 84px;
      width: 240px;
      margin: auto;
      margin-top: 35px;
      // position: absolute;
      // left: 50%;
      // transform: translate(-50%);
      // bottom: -42px;
      border-radius: 42px;
      color: #fff;
      cursor: pointer;
    }
  }
}
</style>